import { getRongIMChatList } from "@/utils/RongIM.js";
import { i18n } from "@/main.js";
export default {
  setCountryList(state, payload) {
    console.log(payload,'payloadpayloadpayloadpayloadpayload');
    state.countryList = payload;
  },
  SET_TOKEN(state, token) {
    state.token = token;
  },
  SET_RONG_TOKEN(state, rongToken) {
    state.rongToken = rongToken;
  },
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo;
  },
  /**
   *
   * @param {*} data 需要处理消息的值
   * @param {*} type send/receive 判断当前为接收还是发送消息
   * @function  用于构建房间最后一条消息，最后一条未读消息的时间戳，以及处理发送和接收到消息时的逻辑
   */
  UPDATE_CHAT_LIST(state, { data, type }) {
    // console.log('zuijou>>>>', data, type, state.chatList)
    let matchingIndex = -1;
    if (!state.chatList || state.chatList.length == 0) {
      getChatThrottle(state);
      return;
    }
    state.chatList.forEach(async (t, i) => {
      // 判断该信息是否为循环到的房间信息
      const isTrue =
        t.conversationType === data.conversationType &&
        t.targetId === data.targetId;
      if (isTrue) {
        matchingIndex = i;
        // 构建该房间最后一条的信息
        t.latestMessage = data;
        t.lastUnreadTime = data.sentTime;
        // 收到的信息的情况
        if (type === "receive") {
          if (data.isMentioned) {
            t.hasMentioned = data.isMentioned;
          }
          // 判断收到的信息是否为当前房间，是的话则接上数据，不是的话则增加未读数
          if (
            state.roomBaseInfo &&
            t.conversationType === state.roomBaseInfo.conversationType &&
            t.targetId === state.roomBaseInfo.targetId
          ) {
            // 如果是群聊则构建该消息发送人的信息，如果是单聊则直接添加进去
            if (data.conversationType === 3) {
              // const groupMemberInfo = await getGroupMemberInfo(data.targetId)
              const groupMemberInfo = state.currentGroupMemberInfo;
              if (groupMemberInfo) {
                const index = groupMemberInfo.findIndex(
                  (e) => Number(e.userId) === Number(data.senderUserId)
                );
                index !== -1
                  ? (data.info = groupMemberInfo[index])
                  : (data.info = {});
              }
            }
            state.receiveMessage.push(data);
          } else {
            t.unreadMessageCount++;
          }
        } else if (type === "send") {
          if (
            t.conversationType === state.roomBaseInfo.conversationType &&
            t.targetId === state.roomBaseInfo.targetId
          ) {
            data.info = state.userInfo;
            state.receiveMessage.push(data);
          }
        }
      }
    });
    // 如果聊天列表不存在改房间，则重新调用聊天室的接口
    if (matchingIndex === -1) {
      getChatThrottle(state);
    }
  },
  SET_CONNECT_STATUS(state, connectStatus) {
    state.connectStatus = connectStatus;
  },
  SET_USER_ID(state, userId) {
    state.userId = userId;
  },
  SET_TEMP_DATA(state, tempData) {
    state.tempData = tempData;
  },
  SET_FRIEND_LIST(state, data) {
    state.friendsList = data;
  },
  SET_SELECT_FRIEND_OPTION(state, data) {
    state.selectFriendOption = data;
  },
  SET_SUBMIT_FRIEND_LIST(state, data) {
    state.submitSelectFriendList = data;
  },
  // 群聊信息
  SET_GROUP_INFO(state, groupInfo) {
    state.groupInfo = groupInfo;
  },
  SET_GROUP_LIST(state, groupList) {
    state.groupList = groupList;
  },
  SET_FRIENDS_INFO(state, friendsInfo) {
    state.friendsInfo = friendsInfo;
  },
  SET_CHAT_LIST(state, chatList) {
    state.chatList = chatList;
  },
  SET_CURRENT_ORDER_INFO(state, data) {
    state.currentOrderInfo = data;
  },
  SET_PRIVATE_CHAT_INFO(state, privateChatInfo) {
    state.privateChatInfo = privateChatInfo;
    state.privateChatInfo = {
      ...state.privateChatInfo,
    };
  },
  SET_GROUP_CHAT_INFO(state, groupChatInfo) {
    state.groupChatInfo = groupChatInfo;
    state.groupChatInfo = {
      ...state.groupChatInfo,
    };
  },
  UP_CHAT_LIST(state, data) {
    if (Array.isArray(state.chatList)) {
      let index = state.chatList.findIndex((o) => o.targetId == data.targetId);
      if (index > -1) {
        state.chatList[index].latestMessage = data;
        state.chatList.unshift(state.chatList.splice(index, 1)[0]);
      } else {
        getRongIMChatList();
      }
    }
  },

  CLEAR_DATA(state, data) {
    state.chatList = [];
    state.token = "";
    state.rongToken = "";
    state.userInfo = {};
    state.userId = "";
    state.friendsList = [];
    state.friendsInfo = {};
    state.groupInfo = {};
    state.groupList = [];
    state.connectStatus = 0;
    state.friendsInformation = {};
    state.currentOrderInfo = {};
    state.groupChatInfo = {};
    state.moduleGroupInfo = {};
    state.pageChatList = [];
    state.privateChatInfo = {};
    state.selectChatOption = {};
    state.selectFriendOption = {};
    state.submitSelectFriendList = [];
    state.typing = false;
    state.roomBaseInfo = {};
    state.OLV1_USER = {};
    // 清除浏览器缓存
    const removeList = ["welcomeShow"];
    removeList.forEach((t) => {
      uni.removeStorage({
        key: t,
      });
    });

    // 退出登录清楚全部缓存的话,会造成融云列表未读数异常
    //  let locale = uni.getLocale()
    //  uni.clearStorageSync()
    //  uni.setLocale(locale)
    //  uni.setStorage({
    //    key: 'UNI_LOCALE',
    //    data: locale,
    //  })
    //  i18n.locale = locale
  },
  SET_Normal_Pic(state, data) {
    state.normalPic = data;
  },
  SET_SELECT_CHAT_OPTION(state, data) {
    state.selectChatOption = data;
  },
  SET_PAGE_CHAT_LIST(state, data) {
    state.pageChatList = data;
  },
  UPDATE_PAGE_CHAT_LIST(state, { index, newData }) {
    state.pageChatList[index] = newData;
  },
  FILTER_PAGE_CHAT_LIST(state, data) {
    state.pageChatList = data.filter((o) => o.info != null);
  },
  SET_ROOM_BASE_INFO(state, roomBaseInfo) {
    state.roomBaseInfo = roomBaseInfo;
  },
  SET_TYPING(state, status) {
    state.typing = state.typing || [];
    status.forEach((item) => {
      state.typing.push(item.targetId);
    });
    state.typing = [...new Set(state.typing)];

    console.log("已读状态的id", state.typing);
  },
  REST_TYPING(state) {
    setTimeout(() => {
      state.typing = [];
    }, 1000);
  },
  SET_OLV1_USER(state, OLV1_USER) {
    state.OLV1_USER = OLV1_USER;
  },
  SET_IS_WEBVIEW(state, isWebview) {
    state.isWebview = isWebview;
  },
};
